<include file="Public/header" /> 

<!--主窗体-->
<div class="container app">
	<!--左侧菜单-->
	<include file="Public/sidebar" /> 
	<!--左侧菜单//-->
	
	<!-- mainCon -->
	<div id="mainCon" class="mainCon">
		<h4>添加分类</h4>
		<form class="form-horizontal" id="myform">
			<div class="form-group">
				<label for="name" class="col-sm-2 control-label">分类名称</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="name" id="name" value="" placeholder="分类名称">
				</div>
			</div>
			
			<div class="form-group">
				<label for="alias" class="col-sm-2 control-label">分类别名</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="alias" id="alias" value="" placeholder="分类别名,方便url识别美化">
				</div>
			</div>
			
			<div class="form-group">
				<label for="sort" class="col-sm-2 control-label">排序</label>
				<div class="col-sm-5">
					<input type="text" class="form-control" name="sort" id="sort" value="100" placeholder="自定义排序，值越大越靠前.">
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-2 control-label">上传图片</label>
				<div class="radio col-sm-5">
					<a class="btn btn-default" id="uploadify">上传图片</a> <span style="margin-top:10px;float:left;color:#999">（仅允许上传1MB以内的图片）</span>
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-2 control-label"></label>
				<div id="showimg">
					
				</div>
				<input type="hidden" id="upimg" name="upimg" value=""/>
			</div>
			
			<div class="form-group">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="submit" class="btn btn-default col-sm-2">保存</button>
				</div>
			</div>
		</form>
		
	</div>
	<!-- mainCon// -->
	
</div>
<!--主窗体//-->

<script src="__PUBLIC__/js/layer/layer.js"></script>
<script src="__PUBLIC__/js/validate/jquery.validate.min.js"></script>
<script src="__PUBLIC__/js/validate/messages_zh.js"></script>
<script src="__PUBLIC__/js/uploadify/jquery.uploadify.min.js"></script>
<script>
// 判断英文字符 
jQuery.validator.addMethod("isEnglish", function(value, element) {       
     return this.optional(element) || /^[A-Za-z0-9]+$/.test(value);       
}, "只能包含英文字符。"); 

$(function(){
	
	$("#myform").validate({
		rules: {
			name : {
				required : true
            },
            alias : {
            	required : true,
            	isEnglish : true
            }
		},
		messages: {
			
		},
		submitHandler : function(form){
			var ajaxUrl = "<{:U('Admin/Shopping/do_addCate')}>";
			 $.post(ajaxUrl,$(form).serialize(),function(r){
    			if (r.error == 1){
    				layer.alert(r.errmsg,{icon: 2});
    			}else{
    				layer.alert(r.errmsg,{icon: 1},function(){
    					window.location.href = "<{:U('Admin/Shopping/cateList',array('k'=>4,'sk'=>3))}>";
    				});
    			}
    			return false;
    		});
        	return false
		}
	});
	
	// 文件上传
	$("#uploadify").uploadify({
	//	'formData': {"uptype":"cate"},
		'swf'           : '__PUBLIC__/js/uploadify/uploadify.swf',
		'uploader'      : "<{:U('Admin/Gallery/uploadImg')}>",
		'fileTypeExts'	: '*.jpg;*.png;*.gif',
		'width'			: 90,
		'buttonText' : '<a class="btn btn-default" id="uploadify">上传图片</a>',
		'hideButton' : true,
		'multi': false,
		'fileObjName' : 'imgFile',
		'fileSizeLimit' : '1MB',
		'onUploadError' : function(back) {
			
		},
		'onUploadStart' : function(file) {

    	},
		'onUploadSuccess' : function(file,data,response) {
			var r = jQuery.parseJSON(data);
			if(r.error == 0){
				var htm = '<div style="width:200px;height:200px;border:1px solid #ccc;overflow:hidden;">';
				htm += '<img src="/'+r.imgUrl+'" style="width:200px;height:200px;float:left;margin:0 auto;padding:5px;">';
				htm += '<div onclick="removeImg();" style="height:30px;width:100%;background:#000;opacity:.6;text-align:center;z-index:3;position:relative;bottom:30px;color:#fff;line-height:30px;float:left;cursor:pointer;">删除</div>';
				htm += '</div>';
				$("#showimg").html(htm);
				$("#upimg").val(r.imgUrl);
			}
		},
	
	});
	
});

function removeImg(){
	$("#showimg").html('');
	$("#upimg").val('');
}
</script>
<style>
.uploadify-queue{
	display:none;
}
</style>
<include file="Public/footer" /> 